package org.springboot.base.demo.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springboot.base.demo.service.RedisService;
import org.springboot.base.user.does.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * TODO
 * @author ZJL
 * @dateTime 2019-02-18 下午9:19:15
 *  
 */
@Service
public class RedisServiceImpl implements RedisService {
	
	private SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd");
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public UserDO getUser(Long userId) {
		// TODO Auto-generated method stub
		UserDO user = new UserDO();
		user.setUserId(10002l);
		user.setUserName("userName10002");
		user.setUserAge(10002);
		user.setUserBirthday(Calendar.getInstance().getTime());
		user.setUserPwd("userPwd10002");
		String sql = "insert into user(user_name,user_pwd,user_age,user_birthday) "
				+ "values('"+user.getUserName()+"',"
				+ "'"+user.getUserPwd()+"',"+user.getUserAge()+",'"
						+ sdf.format(user.getUserBirthday())+"')";
		System.out.println(sql);
		this.jdbcTemplate.update(sql);
		return user;
	}

	@Override
	public UserDO addUser(UserDO user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserDO updateUser(UserDO user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteUser(Long userId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<UserDO> listUser() {
		// TODO Auto-generated method stub
		List<UserDO> list = new ArrayList<UserDO>();
		UserDO user = null;
		for(int i=3;i<10;i++) {
			user = new UserDO();
			user.setUserId(10002l+i);
			user.setUserName("userName1000"+i);
			user.setUserAge(1000+i);
			user.setUserBirthday(Calendar.getInstance().getTime());
			user.setUserPwd("userPwd1000"+i);
			list.add(user);
		}
		return list;
	}

	@Override
	public List<UserDO> listUser(UserDO user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public UserDO getUser(String userName) {
		// TODO Auto-generated method stub
		UserDO user = new UserDO();
		user.setUserId(10001l);
		user.setUserName("userName10001");
		user.setUserAge(10001);
		user.setUserBirthday(Calendar.getInstance().getTime());
		user.setUserPwd("userPwd10001");
		String sql = "insert into user(user_name,user_pwd,user_age,user_birthday) values(?,?,?,?)";
		System.out.println(sql);
		this.jdbcTemplate.update(sql,user.getUserName(),user.getUserPwd(),user.getUserAge(),user.getUserBirthday());
		return user;
	}

}
